package memleak.test.com.testlist;

import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.gzgd.log.LogUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * Created by runrun on 2017/2/13.
 */

public class TabAdapter extends PagerAdapter {
    private static final String TAG = "TabAdapter";
    private List<ChannelCategory> mDatas;
    private ViewFactory mViewFactory;

    public TabAdapter() {
        mDatas = new ArrayList<>();
        mViewFactory = ViewFactory.getViewFactory();
    }

    private static List<LiveChannel> cutIn999(List<LiveChannel> list) {
        LogUtils.d(TAG, "cutIn999");
        if (list == null || list.isEmpty()) return list;
        String categoryID = list.get(0).getCategory();
        if (Constant.FAVORITE_CATEGORY.equals(categoryID)) {
            Collections.sort(list);
            return list;
        }
        LiveChannel channel999 = ChannelRecyclerViewAdapter.getChannelBeanByLogicNum(list, 999);
        if (channel999 == null) return list;
        list.remove(channel999);
        list.add(0, channel999);
        return list;
    }

    void updateData(List<ChannelCategory> channelCategories) {
        if (null == channelCategories || channelCategories.isEmpty()) {
            return;
        }
        for (ChannelCategory category : channelCategories) {
            cutIn999(category.getLiveChannels());
        }
        mDatas.clear();
        mDatas.addAll(channelCategories);
    }

    ChannelCategory getChannelCategory(int position) {
        return mDatas.isEmpty() ? null : mDatas.get(position);
    }

    ChannelCategory getFavoriteCategory() {
        if (mDatas == null || mDatas.isEmpty()) {
            return null;
        }
        ChannelCategory category = null;
        for (int i = 0; i < mDatas.size(); i++) {
            category = mDatas.get(i);
            if (category.getCategoryID().equals(Constant.FAVORITE_CATEGORY)) {
                return category;
            }
        }
        return category;
    }

    void updateFavorites(LiveChannel channel) {

        if (channel == null) {
            return;
        }

        Log.i(TAG, "channel.name:" + channel.getcName() + "/" + channel.getcNo() + " / channel.getduration:" + channel.getTotalDuration());

        if (!mDatas.isEmpty()) {
            for (int i = 0; i < mDatas.size(); i++) {
                ChannelCategory category = mDatas.get(i);
                String categoryID = category.getCategoryID();
                if (!TextUtils.isEmpty(categoryID) && categoryID.equals(Constant.FAVORITE_CATEGORY)) {
                    List<LiveChannel> channels = category.getLiveChannels();
                    if (channels == null) {
                        return;
                    }
                    if (channels.isEmpty()) {
                        channels.add(channel);
                        break;
                    } else {
                        boolean flag = false;
                        for (int j = 0; j < channels.size(); j++) {
                            LiveChannel channel1 = channels.get(j);
                            String vid = channel1.getVideoId();
                            if (vid.equals(channel.getVideoId())) {
                                channel1.setTotalDuration(channel.getTotalDuration());
                                flag = true;
                                break;
                            }
                        }
                        if (!flag) {
                            channels.add(channel);
                        }
                    }
                    for (int k = 0; k < channels.size(); k++) {
                        Log.i(TAG, "打印 - name:" + channels.get(k).getcName() + " / " + "duration:" + channels.get(k).getTotalDuration());
                    }
                }
            }
        }
    }


    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void startUpdate(ViewGroup container) {
        super.startUpdate(container);
    }

    @Override
    public int getItemPosition(Object object) {
        LiveApplication app = LiveApplication.getGlobalContext();
        if (app.isFlagUpdateData()) {
            app.setFlagUpdateData(false);
            return POSITION_NONE;
        }
        ChannelLayout layout = (ChannelLayout) object;
        if (layout != null) {
            if (layout.getCategoryID().equals(Constant.FAVORITE_CATEGORY)) {
                return POSITION_NONE;
            } else {
                return POSITION_UNCHANGED;
            }
        } else {
            return POSITION_NONE;
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    public List<LiveChannel> getDatas(int pos) {
        if (pos >= mDatas.size()) {
            return null;
        }
        return mDatas.get(pos).getLiveChannels();
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {

        ChannelLayout child = (ChannelLayout) LayoutInflater.from(container.getContext()).inflate(R.layout.channel_layout_layout, null);
        child.setTag(position);
        ChannelCategory bean = getChannelCategory(position);
        container.addView(child);
        child.setCategoryID(bean.getCategoryID());
        child.updateView(bean.getLiveChannels());
        LiveApplication mApp = (LiveApplication) container.getContext().getApplicationContext();
        int pagePos = mApp.getPagePos();
        if (position == pagePos && pagePos != 0) {
            int channelPos = mApp.getChannelPos();
            child.setCurrentItem(channelPos);
        }
        return child;
    }
}
